package com.huafeng.controller;

import com.huafeng.annotations.Log;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysDept;
import com.huafeng.model.enums.OperationTypeEnum;
import com.huafeng.model.enums.ResultCodeEnum;
import com.huafeng.model.exception.BusinessException;
import com.huafeng.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 花风
 * @date 2023/8/19 11:57
 * @Description SysDeptController
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("admin/system/sysDept")
public class SysDeptController {
    @Resource
    private SysDeptService sysDeptService;

    @Log(model = "部门管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取部门列表")
    @GetMapping("findNodes")
    public Result findNodes(){
        return sysDeptService.findNodes();
    }

    @Log(model = "部门管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取部门详情信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id必须不为空且大于0");
        SysDept sysDept = sysDeptService.query().eq("id", id).one();
        if (sysDept == null){
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        return Result.ok(sysDept);
    }

    @Log(model = "部门管理",operationType = OperationTypeEnum.INSERT)
    @ApiOperation("新增部门")
    @PostMapping("save")
    public Result save(@RequestBody SysDept sysDept){
        boolean save = sysDeptService.save(sysDept);
        return CheckUtils.check(save);
    }

    @Log(model = "部门管理",operationType = OperationTypeEnum.UPDATE)
    @ApiOperation("更改部门")
    @PutMapping("update")
    public Result update(@RequestBody SysDept sysDept){
        boolean update = sysDeptService.updateById(sysDept);
        return CheckUtils.check(update);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.STATUS)
    @ApiOperation("更新用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        Assert.isTrue(status != null && status >= 0,"status is illegal");
        return sysDeptService.updateStatus(id,status);
    }
}
